"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:RelmoduleIndexCtrl
 * @description
 * # RelmoduleIndexCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "RelmoduleIndexCtrl",
    function($scope, $routeParams, Util, PagingService, DataService, localStorageService, openApi) {
      // 后退时候会清除要记住
      var TVType = Util.getTVType();
      var data = localStorageService.get("relData");
      $scope.onClickTab = function(item) {
        if ($scope.currentTabName != item.name) {
          $scope.currentTabName = item.name;
          getRelAttrData(item.columns);
          $scope.formKey=item.key
          getRelData(item);
        }
      };
      $scope.isActiveTab = function(item) {
        return item.name == $scope.currentTabName;
      };
      function getTabWidth(data) {
        var count = 0;
        data.forEach(function(item) {
          count += item.name.length;
        });
        var cwidth = count * 13;
        var pwidth = data.length * 10;
        $scope.emWidth = cwidth + pwidth +20+ "px";
      }
      /**
       * 解析table key名称
       * @param  {[type]} item [description]
       * @return [type]        [description]
       */
      function getRelAttrData(columns) {
        var attrObj = [];
        var colsArr = columns.split(";");
        colsArr.forEach(function(item, index) {
          var colArr = item.split(",");
          var relItemObj = {};
          relItemObj.key = colArr[2];
          relItemObj.value = colArr[0];
          relItemObj.format = colArr[1];
          relItemObj.extend = colArr[3];
          attrObj.push(relItemObj);
        });
        $scope.attrObj = attrObj;
      }
      // 获取关联列表
      function getRelData(item) {
        $scope.taskList = new PagingService(
          openApi.ip +
            item.actionPath +
            "?" +
            item.formalPar +
            "=" +
            data[item.realPar]
        );
      }
      $scope.goback = function() {
        Util.setTVType(TVType);
        Util.goBack();
      };
      function init() {
        DataService.getData(openApi.getrelList, {
          key: $routeParams.formKey
        }).then(function(data) {
          $scope.tabList = data;
          getTabWidth(data);
          $scope.onClickTab(data[0]);
        });
      }
      init();
    }
  );
